# BigData course project
# Serial version of training algorithm for SOM
# Train set class

import sys
from util import *

class TrainSet:
    # constructor knows how to build from file
    def __init__(self, dim, size, file):
        self.dim = dim
        self.size = size
        try:
            f = open(file, "r")
        except IOError:
            log("Could not read trainset file %s\n", file)
            sys.exit(11)
        try:
            self.c = [map(int, f.readline().split()) for i in xrange(size)]    
        except IOError:
            log("Could not read trainset line %d\n", i)
            sys.exit(12)
        for i in xrange(size):
            if len(self.c[i]) != dim:
                log("Line %d of trainset has invalid length (%d != %d)\n", 
                    i, len(self.c[i]), dim)
                sys.exit(13)                
        f.close()

    # dump the trainset (one train vector per line)    
    def dump(self, file):
        dump_vector_list("trainset", self.c, file)

